package ltd.sshine.networkserver.port;

import ltd.sshine.networkcommon.config.ProxyConfig;
import ltd.sshine.networkserver.exception.PortCheckException;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component
public class PortUtils {


    private final Set<PortHandler> portHandlers;

    public PortUtils(Set<PortHandler> portHandlers) {
        this.portHandlers = portHandlers;
    }

    public int genPortUtils(ProxyConfig proxyConfig, List<Integer> ignore) throws PortCheckException {
        int port;
        for (String s : proxyConfig.getPort()) {
            boolean noCheck = true;
            for (PortHandler portHandler : portHandlers) {
                if (portHandler.check(s)) {
                    if ((port = portHandler.genPort(s, ignore)) != -1) return port;
                    noCheck=false;
                }
            }
            if (noCheck)
                throw new PortCheckException("无法处理:" + s);
        }
        return -1;
    }


}
